package com.lingyun.generator.output;

import com.lingyun.generator.bean.Config;
import com.lingyun.generator.bean.TableInfo;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * Created by bain.wang on 2016/1/11.
 */
public class VelocityOutput {

    static final String VM_JAVA_ENTITY = "Entity.java.vm";
    static final String VM_JAVA_DAO = "EntityRepository.java.vm";
    static final String VM_JAVA_SERVICE = "EntityService.java.vm";
    static final String VM_JAVA_SERVICE_IMPL = "EntityServiceImpl.java.vm";
    static final String VM_JAVA_CONTROLLER = "EntityController.java.vm";

    static final String VM_LIST = "vm.list.vm";
    static final String VM_EDIT = "vm.edit.vm";
    static final String VM_ADD = "vm.add.vm";

    static final String VM_JS_ADD = "js.add.vm";
    static final String VM_JS_LIST = "js.list.vm";
    static final String VM_JS_EDIT = "js.edit.vm";


    public static void execute(TableInfo tableInfo, Config config) {
        if (config.isGeneratorEntity()) {
            output(tableInfo, config, VM_JAVA_ENTITY, tableInfo.getBigJavaName() + ".java");
        }

        if(config.isGeneratorDao()) {
            output(tableInfo, config, VM_JAVA_DAO, tableInfo.getBigJavaName() + "Repository.java");
        }

        if(config.isGeneratorService()) {
            output(tableInfo, config, VM_JAVA_SERVICE, tableInfo.getBigJavaName() + "Service.java");
            output(tableInfo, config, VM_JAVA_SERVICE_IMPL, tableInfo.getBigJavaName() + "ServiceImpl.java");

        }

        if(config.isGeneratorController()){
            output(tableInfo, config, VM_JAVA_CONTROLLER, tableInfo.getBigJavaName() + "Controller.java");

        }

        if(config.isGeneratorVm()){

            output(tableInfo, config, VM_LIST, "list-" + tableInfo.getName() + ".vm");
            output(tableInfo, config, VM_ADD, "add-" + tableInfo.getName() + ".vm");
            output(tableInfo, config, VM_EDIT, "edit-" + tableInfo.getName() + ".vm");

            output(tableInfo, config, VM_JS_LIST, "list-" + tableInfo.getName() + ".js");
            output(tableInfo, config, VM_JS_ADD, "add-" + tableInfo.getName() + ".js");
            output(tableInfo, config, VM_JS_EDIT, "edit-" + tableInfo.getName() + ".js");
        }



    }


    private static void output(TableInfo tableInfo, Config config, String filename, String outputFilename) {
        try {
            VelocityEngine ve = new VelocityEngine();
            ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
            ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

            ve.init();

            Template template = ve.getTemplate("template/" + filename, "utf-8");
            String templateFilePath = template.getName();


            VelocityContext ctx = new VelocityContext();

            ctx.put("tableInfo", tableInfo);
            ctx.put("config", config);

            String rootPath = config.getOutputPath();

            if (StringUtils.equals(filename, VM_JAVA_ENTITY) && StringUtils.isNotEmpty(config.getEntityOutputPath())) {
                rootPath = config.getEntityOutputPath();
            }

            if (StringUtils.equals(filename, VM_JAVA_DAO) && StringUtils.isNotEmpty(config.getDaoOutputPath())) {
                rootPath = config.getDaoOutputPath();
            }

            if (StringUtils.equals(filename, VM_JAVA_SERVICE) && StringUtils.isNotEmpty(config.getServiceOutputPath())) {
                rootPath = config.getServiceOutputPath();
            }

            if (StringUtils.equals(filename, VM_JAVA_SERVICE_IMPL) && StringUtils.isNotEmpty(config.getServiceOutputPath())) {
                rootPath = config.getServiceOutputPath() + "impl/";
            }

            if (StringUtils.equals(filename, VM_JAVA_CONTROLLER) && StringUtils.isNotEmpty(config.getControllerOutputPath())) {
                rootPath = config.getControllerOutputPath();
            }

            if (StringUtils.equals(filename, VM_LIST) && StringUtils.isNotEmpty(config.getVmOutputPath())) {
                rootPath = config.getVmOutputPath() + tableInfo.getName() + "/";
            }

            if (StringUtils.equals(filename, VM_ADD) && StringUtils.isNotEmpty(config.getVmOutputPath())) {
                rootPath = config.getVmOutputPath() + tableInfo.getName() + "/";
            }

            if (StringUtils.equals(filename, VM_EDIT) && StringUtils.isNotEmpty(config.getVmOutputPath())) {
                rootPath = config.getVmOutputPath() + tableInfo.getName() + "/";
            }

            if (StringUtils.equals(filename, VM_JS_LIST) && StringUtils.isNotEmpty(config.getJsOutputPath())) {
                rootPath = config.getJsOutputPath() + tableInfo.getName() + "/";
            }

            if (StringUtils.equals(filename, VM_JS_ADD) && StringUtils.isNotEmpty(config.getJsOutputPath())) {
                rootPath = config.getJsOutputPath() + tableInfo.getName() + "/";
            }

            if (StringUtils.equals(filename, VM_JS_EDIT) && StringUtils.isNotEmpty(config.getJsOutputPath())) {
                rootPath = config.getJsOutputPath() + tableInfo.getName() + "/";
            }


            File output = new File(rootPath + outputFilename);
            if (!output.getParentFile().exists()) {
                output.getParentFile().mkdir(); //创建输出流
            }

            System.out.println("");
            System.out.println("");
            System.out.println("################################################################");
            System.out.println(JSONObject.fromObject(tableInfo));
            System.out.println(JSONObject.fromObject(config));
            System.out.println(templateFilePath);
            System.out.println(output.getAbsolutePath());


            FileOutputStream outputStream = new FileOutputStream(output);
            OutputStreamWriter writer = new OutputStreamWriter(outputStream, Charset.forName("utf-8"));
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            template.merge(ctx, bufferedWriter);
            bufferedWriter.flush();
            outputStream.close();
            bufferedWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
